import { objectType, extendType, stringArg } from "@nexus/schema";
import { getUserId } from "../utils";

export const Project = objectType({
  name: "Project",
  definition(t) {
    t.string("id");
    t.string("name");
    t.field("user", { nullable: true, type: "User" });
    t.field("folders", { nullable: true, type: "Folder", list: true });
    t.field("files", { nullable: true, type: "File", list: true });
  },
});

export const ProjectQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("project", {
      type: "Project",
      args: {
        name: stringArg({ required: true }),
      },
      resolve(__root, args, ctx) {
        return ctx.db.project.findOne({
          where: { name: args.name },
          include: { folders: true },
        });
      },
    });
  },
});

export const ProjectsQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("projects", {
      type: "Project",
      list: true,
      resolve(__root, args, ctx) {
        return ctx.db.project.findMany({ include: { folders: true } });
      },
    });
  },
});

export const CreateProjectMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("createProject", {
      type: "Project",
      args: {
        name: stringArg({ required: true }),
      },
      resolve(_root, args, ctx) {
        console.log("enter create project mutation");
        const project = {
          name: args.name,
          user: { connect: { id: ctx.userId } },
        };
        return ctx.db.project.create({ data: project });
      },
    });
  },
});

// create project with folder and "main.py"
export const CreateProjectInitMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("createProjectInit", {
      type: "Project",
      args: {
        name: stringArg({ required: true }),
      },
      resolve: async (_root, args, ctx) => {
        const project = {
          name: args.name,
          user: { connect: { id: ctx.userId } },
        };

        const project_create_result = await ctx.db.project.create({
          data: project,
        });
        console.log("project create result:", project_create_result);

        // create root folder
        const root_folder = {
          name: args.name, // same to project name
          project: { connect: { id: project_create_result.id } },
        };
        console.log("create root folder:", root_folder);

        const folder_create_result = await ctx.db.folder.create({
          data: root_folder,
        });
        console.log("folder create result:", folder_create_result);
        if (!folder_create_result) {
          throw new Error("create root folder error");
        }

        // create "main.py"
        const file = {
          name: "main.py",
          extension: "py",
          content: `print("Hello, I'm Python!")`,
          folder: { connect: { id: folder_create_result.id } },
        };
        const file_create_result = await ctx.db.file.create({ data: file });
        if (!file_create_result) {
          throw new Error("create main.py file error");
        }

        return project_create_result;
      },
    });
  },
});

export const DeleteProjectMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("deleteProject", {
      type: "Project",
      args: {
        name: stringArg({ required: true }),
      },
      resolve(_root, args, ctx) {
        return ctx.db.project.delete({
          where: { name: args.name },
        });
      },
    });
  },
});
